<template>
  <el-container style="width: 100%;">
    <el-header style="height: 47px;">
      <p>人员档案列表</p>
    </el-header>
    <el-main>
      <el-row class="btnGroup">
        <el-button class="twoBtn" @click="addPerson"><span>添加</span>
          <i class="el-icon-plus"></i>
        </el-button>
       <!-- <el-button class="twoBtn" @click="editPerson"><span>编辑</span><i class="el-icon-edit"></i></el-button>-->
        <el-button class="twoBtn" @click="delPerson"><span>删除</span><i class="el-icon-delete"></i></el-button>
        <!--<el-button class="fourBtn" @click="selRoad"><span>选择路段</span><i class="el-icon-search"></i></el-button>-->
      </el-row>
      <el-table
        :data="tableDatas.list"
        style="width:100%"
        height="600"
        fitcell-style
        ref="multipleTable"
        tooltip-effect="dark"
        stripe
        @selection-change="handleSelectionChange"
        v-loading="loading"
      >
        <el-table-column fixed align="center" type="selection" width="50"></el-table-column>
        <el-table-column fixed sortable align="center"  prop="record_name" label="姓名" width="150"></el-table-column>
        <el-table-column sortable align="center" prop="record_no" label="工号" width="100"></el-table-column>
        <el-table-column sortable align="center" prop="record_dwname" label="单位名称" width="180"></el-table-column>
        <el-table-column sortable align="center" prop="record_sfzhm" label="身份证号码" width="180"></el-table-column>
        <el-table-column sortable align="center" prop="record_lxdh" label="联系电话" width="150"></el-table-column>
        <el-table-column sortable align="center" prop="record_zwname" label="职务" width="150"></el-table-column>
        <el-table-column sortable align="center" prop="record_unitname" label="收费路段" width="150"></el-table-column>
        <el-table-column sortable align="center" prop="record_rzrq" label="入职日期" width="180"></el-table-column>
        <el-table-column sortable align="center" prop="record_xbname" label="性别" width="100"></el-table-column>
        <el-table-column sortable align="center" prop="record_csrq" label="出生日期" width="150"></el-table-column>
        <el-table-column fixed="right" sortable align="center" prop="record_statename" label="状态" width="100"></el-table-column>
        <el-table-column fixed="right" align="center" width="100" label="操作">
          <template slot-scope="scope">
            <el-dropdown trigger="click" @command="handleCommand">
              <el-button class="downBtn" @click="changeRow(scope.row)">
                <i class="el-icon-arrow-down"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="search">查看路段</el-dropdown-item>
                <el-dropdown-item command="edit">编辑信息</el-dropdown-item>
                <el-dropdown-item command="del">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer>
      <el-pagination
        background
        layout="total,sizes,prev,pager,next,jumper"
        :total="dataCount"
        :page-sizes="[10,20,30,50]"
        :page-size="dataPageSize"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        style="margin-top: 16px;"
      ></el-pagination>
    </el-footer>
    <el-dialog title="路段选择" :visible.sync="visible">
      <el-form :model="selectRoads" ref="selectRoads" :label-position="labelPosition" label-width="100px">
        <el-row style="text-align: center">
          <el-col :xs="6" :sm="6" :lg="6"><p></p></el-col>
          <el-col :xs="12" :sm="12" :lg="12">
            <el-form-item label="单位名称">
              <el-input v-model="selectRoads.dw_name" @click.native="roadPop"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="text-align: center">
          <el-col :xs="6" :sm="6" :lg="6"><p></p></el-col>
          <el-col :xs="12" :sm="12" :lg="12">
            <el-form-item label="收费路段">
              <el-select v-model="selectRoads.roads" multiple collapse-tags placeholder="请选择">
                <el-option v-for="item in haveRoad" :key="item.chargeUnitId" :value="item.chargeUnitId" :label="item.chargeUnitName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="8" :sm="8" :lg="8"><p></p></el-col>
          <el-col :xs="12" :sm="12" :lg="12">
            <el-button type="primary" :loading="editRoadsBtn" @click="submitRoad">确定</el-button>
            <el-button style="margin-left: 36px" @click="closeRoad">取消</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </el-container>
</template>

<script>
  import { delPersonnel, selUnitByOrg, editUnit } from '../../../api/personnelFile'
  import { getNum } from '../../../api/public/searchUnit'

  export default {
    name: 'person-child-table',
    data() {
      return {
        labelPosition: 'left',
        pageNum: 1, // 当前页码
        dataCount: 0, // 数据总条数
        dataPageSize: 0, // 每页显示条数
        multipleTable: [],
        changeVal: '',
        visible: false,
        editRoadsBtn: false,
        selectRoads: {
          roads: [],
          dw_name: '',
          record_name: '',
          dw: '',
          record_sfzhm: ''
        },
        itemRoad: [],
        haveRoad: []
      }
    },
    props: ['dw_name3', 'loading', 'tableDatas'],
    watch: {
      dw_name3: function(val) {
        this.selectRoads.dw_name = val.orgname
        selUnitByOrg(val.orgno).then(res => {
          this.haveRoad = res
        })
      },
      pageNum: function(val) {
        this.$emit('pageNums', val)
      },
      tableDatas: function(val) {
        this.dataCount = val.count
        this.dataPageSize = val.pageSize
      }
    },
    methods: {
      handleSelectionChange: function(val) {
        this.multipleTable = val
      },
      handleCurrentChange: function(val) {
        this.pageNum = val
      },
      handleSizeChange(val) {
        this.$emit('sizePage', val)
      },
      /* 添加 */
      addPerson: function() {
        this.$emit('addBoolean', true)
      },
      /* 删除 */
      delPerson: function() {
        if (this.multipleTable.length === 0) {
          this.$message({
            type: 'warning',
            message: '请选择至少一条记录'
          })
          return
        }
        this.$confirm('此操作将永久删除选中的记录,是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let delData = []
          for (var i = 0; i < this.multipleTable.length; i++) {
            delData.push(this.multipleTable[i].record_sfzhm)
          }
          delPersonnel(delData).then(res => {
            this.$emit('delSearch', getNum())
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      /* 选择路段 */
      selRoad: function() {
        if (this.multipleTable.length === 0) {
          this.$message({
            type: 'warning',
            message: '请选择一条记录'
          })
          return
        } else if (this.multipleTable.length === 1) {
          selUnitByOrg(this.multipleTable[0].record_dw).then(res => {
            this.haveRoad = res
            this.selectRoads.record_sfzhm = this.multipleTable[0].record_sfzhm
            this.selectRoads.record_name = this.multipleTable[0].record_name
            this.selectRoads.roads = this.multipleTable[0].record_unit === null ? [] : this.multipleTable[0].record_unit
            this.selectRoads.dw_name = this.multipleTable[0].record_dwname
            this.selectRoads.dw = this.multipleTable[0].record_dw
            this.visible = true
          })
        } else {
          this.$message({
            type: 'warning',
            message: '请选择一条记录'
          })
          return
        }
      },
      /* 提交 */
      submitRoad: function() {
        this.editRoadsBtn = true
        editUnit(this.selectRoads).then(res => {
          this.editRoadsBtn = false
          this.$emit('update:dw_name3', '')
          this.visible = false
          this.$message({
            type: 'success',
            message: '修改成功'
          })
          this.$emit('editRoad', getNum())
        })
      },
      /* 关闭窗口 */
      closeRoad: function() {
        this.$emit('update:dw_name3', '')
        this.visible = false
      },
      /* 打开单位弹出框 */
      roadPop: function() {
        this.$emit('roadPop', true)
      },
      handleCommand(val) {
        if (val === 'search') {
          selUnitByOrg(this.changeVal.record_dw).then(res => {
            this.haveRoad = res
            this.selectRoads.record_sfzhm = this.changeVal.record_sfzhm
            this.selectRoads.record_name = this.changeVal.record_name
            this.selectRoads.roads = this.changeVal.record_unit === null ? [] : this.changeVal.record_unit
            this.selectRoads.dw_name = this.changeVal.record_dwname
            this.selectRoads.dw = this.changeVal.record_dw
            this.visible = true
          })
        } else if (val === 'edit') {
          this.$emit('editBoolean', [true, this.changeVal])
        } else {
          this.$confirm('此操作将永久删除选中的记录,是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let delData = []
            delData.push(this.changeVal.record_sfzhm)
            delPersonnel(delData).then(res => {
              this.$emit('delSearch', getNum())
              this.$message({
                type: 'success',
                message: '删除成功'
              })
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
        }
      },
      changeRow(val) {
        this.changeVal = Object.assign({}, val) // copy obj
      }
    }
  }
</script>

<style scoped>
  .el-dropdown{
    width: 33px;
    height: 19px;
  }
  .el-dropdown > .el-button{
    width: 33px;
    height: 19px;
    padding: 3px 9px;
  }
  .el-main{
    padding: 0;
    margin-top: 24px;
    margin-left: 24px;
  }
  .el-header p{
    font-family: MicrosoftYaHei;
    font-size: 13px;
    margin-top: 22px;
    color: #7F8CA6;
  }
  .el-header{
    padding:0;
    margin-left: 24px;
    margin-right: 24px;
    border-bottom: 1px solid #E1E1E1;
  }
  .btnGroup{
  }
  .btnGroup > .twoBtn{
    width: 64px;
    height:24px;
    padding: 4px 20px;
    border: 1px solid #409eff;
    font-family: MicrosoftYaHei;
    color: #fff;
    font-size: 12px;
  }
  .btnGroup > .fourBtn{
    width: 64px;
    height:24px;
    padding: 4px 8px;
    border: 1px solid #409eff;
    font-family: MicrosoftYaHei;
    color: #fff;
    font-size: 12px;
  }
  .btnGroup > .el-button span{
    display: none;
  }
  .btnGroup > .el-button:hover{
    background-color: #409EFF;
  }
  .btnGroup > .el-button:hover span{
    display: block;
  }
  .btnGroup > .el-button:hover i{
    display: none;
  }
  .el-button i{
    color: #409eff;
  }
  .el-footer{
    text-align: right;
  }
</style>
